package com.syh.jn.scm.domain.supplier.entity.base.logistics;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.repository.finance.*;
import com.syh.jn.scm.domain.supplier.repository.logistics.LogisticsBillReadRepository;
import com.syh.jn.scm.dto.supplier.base.logistics.LogisticsMto;
import com.syh.jn.scm.dto.supplier.base.logistics.LogisticsSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 物流
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Logistics extends AbstractSimpleAggregate<Long> {
  public Logistics (){}

  /**
   * 构造方法增加物流
   * @param sto
   */
  public Logistics (LogisticsSto sto){
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    super.initDefault();
    this.status = Short.valueOf("1");
    ExtBeanUtils.copyProperties(sto, this);
  }
  /**
   * 修改经营主体
   * @param mto
   */
  public void modify(LogisticsMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }
  /**
   * 数据销毁
   */
  public void destroy() {
//    //查看销售单是否有数据存在
//    SaleOrderReadRepository saleOrder = (SaleOrderReadRepository) SpringContextUtils.getBean("saleOrderReadRepositoryMbImpl");
//    Wrapper saleWrapper = Wrapper.build();
//    saleWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
//    Integer saleCount = saleOrder.getCount(saleWrapper);
//    if (saleCount > 0) {
//      throw new BusinessException("销售单存在该数据,删除失败");
//    }
//    //查看销售退货是否有数据存在
//    SaleReturnOrderRepositoryImpl saleReturn = (SaleReturnOrderRepositoryImpl) SpringContextUtils.getBean(SaleReturnOrderRepositoryImpl.class);
//    Wrapper saleReturnWrapper = Wrapper.build();
//    saleReturnWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
//    Integer saleReturnCount = saleReturn.getCountView(saleReturnWrapper);
//    if (saleReturnCount > 0) {
//      throw new BusinessException("销售退货单存在该数据,删除失败");
//    }
//    //查看采购是否有数据存在
//    PurchaseOrderReadRepository purchase = (PurchaseOrderReadRepository) SpringContextUtils.getBean("purchaseOrderReadRepositoryMbImpl");
//    Wrapper purchaseWrapper = Wrapper.build();
//    purchaseWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
//    Integer purchaseCount = purchase.getCount(purchaseWrapper);
//    if (purchaseCount > 0) {
//      throw new BusinessException("采购单存在该数据,删除失败");
//    }
//    //查看采购退货是否有数据存在
//    PurchaseReturnOrderRepositoryImpl purchaseReturn = (PurchaseReturnOrderRepositoryImpl) SpringContextUtils.getBean(PurchaseReturnOrderRepositoryImpl.class);
//    Wrapper purchaseReturnWrapper = Wrapper.build();
//    purchaseReturnWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
//    Integer purchaseReturnCount = purchaseReturn.getCountView(purchaseReturnWrapper);
//    if (purchaseReturnCount > 0) {
//      throw new BusinessException("采购退货单存在该数据,删除失败");
//    }
    LogisticsBillReadRepository logisticsBillReadRepository = (LogisticsBillReadRepository) SpringContextUtils.getBean("logisticsBillRepositoryImpl");
    Wrapper logisticsBillWrapper = Wrapper.build().append(Restrictions.eq("logisticsCompanyId", this.id));
    if (logisticsBillReadRepository.getCount(logisticsBillWrapper) > 0) {
      throw new BusinessException("该物流公司已生成物流单,删除失败");
    }
    //查看对账单是否有数据存在
    StatementAccountBillReadRepository statementBill = (StatementAccountBillReadRepository) SpringContextUtils.getBean(StatementAccountBillReadRepository.class);
    Wrapper statementBillWrapper = Wrapper.build();
    statementBillWrapper.append(Restrictions.eq("customerId", this.id));
    statementBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer statementCount = statementBill.getCount(statementBillWrapper);
    if (statementCount > 0) {
      throw new BusinessException("对账单存在该数据,删除失败");
    }
    //查看费用单是否有数据存在
    CostBillReadRepository costBill = (CostBillReadRepository) SpringContextUtils.getBean(CostBillReadRepository.class);
    Wrapper costBillWrapper = Wrapper.build();
    costBillWrapper.append(Restrictions.eq("customerId", this.id));
    costBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer costCount = costBill.getCount(costBillWrapper);
    if (costCount > 0) {
      throw new BusinessException("费用单存在该数据,删除失败");
    }
    //查看收入单是否有数据存在
    IncomeBillReadRepository incomeBill = (IncomeBillReadRepository) SpringContextUtils.getBean(IncomeBillReadRepository.class);
    Wrapper incomeBillWrapper = Wrapper.build();
    incomeBillWrapper.append(Restrictions.eq("customerId", this.id));
    incomeBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer incomeCount = incomeBill.getCount(incomeBillWrapper);
    if (incomeCount > 0) {
      throw new BusinessException("收入单存在该数据,删除失败");
    }
    //查看收款单是否有数据存在
    ReceiptBillReadRepository receiptBill = (ReceiptBillReadRepository)SpringContextUtils.getBean(ReceiptBillReadRepository.class);
    Wrapper receiptBillWrapper = Wrapper.build();
    receiptBillWrapper.append(Restrictions.eq("customerId", this.id));
    receiptBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer receiptCount = receiptBill.getCount(receiptBillWrapper);
    if (receiptCount > 0) {
      throw new BusinessException("收款单存在该数据,删除失败");
    }
    //查看付款单是否有数据存在
    PaymentBillReadRepository paymentBill = (PaymentBillReadRepository) SpringContextUtils.getBean(PaymentBillReadRepository.class);
    Wrapper paymentBillWrapper = Wrapper.build();
    paymentBillWrapper.append(Restrictions.eq("customerId", this.id));
    paymentBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer paymentCount = paymentBill.getCount(paymentBillWrapper);
    if (paymentCount > 0) {
      throw new BusinessException("付款单存在该数据,删除失败");
    }
  }
  /**
   * 启用
   */
  public void enabled() {
    this.status = Short.valueOf("1");
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = Short.valueOf("-1");
  }
  /**
   * 物流公司名字
   */
  private String name;
  /**
   * 公司的联系电话
   */
  private String telephoneNum;
  /**
   * 是否提供冷链车: -1不提供,1提供
   */
  private Short isProvide;
  /**
   * 状态: -1停用, 1可用
   */
  private Short status;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 期初应收总额
   */
  private BigDecimal beginReceiveTotalAmount;
  /**
   * 期初应付总额
   */
  private BigDecimal beginPaymentTotalAmount;
}
